layui.config({
    base: path + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['tree', 'util', 'index', 'form', 'upload', 'laydate', 'jquery', 'selectN', 'selectM'], function () {
    var tree = layui.tree
        , layer = layui.layer
        , user = parent.userObj
        , $ = layui.$
        , selectN = layui.selectN
        , selectM = layui.selectM
        , form = layui.form
        , upload = layui.upload
        , laydate = layui.laydate;

    var date = new Date();
    var maxdate = date.getFullYear() + '-' + (parseInt(date.getMonth()) + 1) + '-' + date.getDate();
    /**
     * 日期控件
     */
    var ins = laydate.render({
        elem: "#birthday"
        , trigger: 'click'
        , type: 'date'
        , min: '1900-1-1'
        , max: maxdate
        , btns: ['clear', 'confirm']
        , ready: function () {
            //ins.hint('日期可选值设定在 <br> 1900-1-1 到 '+maxdate+'之间');
        }
    });
    /**
     * 文件上传
     */
    upload.render({
        elem: '#layuiadmin-upload-useradmin'        //文件上传标签id
        , url: path + '/main/user/headImgUpload.json'       //文件上传的url
        , accept: 'images'      //打开模态框所能选取的文件类型
        , method: 'post'        //post请求
        , size: 500         //文件大小
        , acceptMime: 'image/*'     //打开模态框筛选下拉框的选项
        , done: function (res) {        //文件上传成功之后执行的操作
            $(this.item).prev("input").val(res.fileId);
            $("#uploadImg").attr("src", path + res.src);
        }, error: function () {         //出现异常执行的操作
            layer.msg('文件上传接口异常,请联系开发者！');
        }
    });
    /**
     * 填充数据
     */
    $(".headImg").attr("src", path + user.userInfo.headImg.filePath).bind("click", function () {
        var url = path + user.userInfo.headImg.filePath;
        var img_infor = "<img src='" + url + "' id='bigImg' title='点击图片隐藏'/>";
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['auto', 'auto'],
            shadeClose: true,
            time: 3000,
            content: img_infor,
            success: function (layero, index) {
                $("#bigImg").bind("click", function () {
                    layer.close(index)
                })
            }
        });
    });
    $("#headImgId").val(user.userInfo.headImgId);
    $(".userId").val(user.userInfo.userId);
    $(".userName").val(user.userInfo.userName);
    $(".nickName").val(user.userInfo.nickName);
    $(".email").val(user.userInfo.email);
    $(".phone").val(user.userInfo.phone);
    var sexId = user.userInfo.sex == "男" ? 0 : 1;
    $("input[type=radio]:eq(" + sexId + ")").next("div").click();
    $(".age").val(user.userInfo.age);
    $(".roleNames").val(user.userInfo.roleNames);
    $(".birthday").val(user.userInfo.birthday.substring(0, user.userInfo.birthday.indexOf(' ')));
    if (user.userInfo.status == "0" && $(".layui-form-switch").length != 0) {
        $(".layui-form-switch").click();
    }
    var rolesIns = $("#roles").length == 0 ? "" : selectM({
        elem: '#roles',
        data: user.rolesSelected,
        max: 2, //默认为5
        name: 'rolesName',
        selected: user.rolesCheckedData, //数组，数组的值是选择的id
        delimiter: ',', //参数分隔符
        default:[user.default],
        field: {
            idName: 'id',
            titleName: 'name'
        },
        success: function (e) {
            if (permissionsIns != undefined) {
                /*重新为权限赋值*/
                permissionsIns.set([]);
                $.ajax({
                    url: path + "/main/user/getPermissionByRoles.json",
                    type: "GET",
                    dataType: "JSON",
                    data: {"roles": $("input[name=rolesName]").val()},
                    success: function (data) {
                        setPermissionsInsSelected(data);
                    },
                    error: function () {
                        layer.msg('获取权限接口异常,请联系开发者！');
                    }
                });
            }
        }
    });

    /**
     * permissions角色多选标签
     */
    var permissionsIns = $("#permissions").length == 0 ? "" : selectM({
        elem: '#permissions',
        data: user.permissionSelected,
        max: user.permissionSelected.length,
        name: 'permissionsName',
        selected: user.permissionsCheckedData,
        delimiter: ',',
        field: {
            idName: 'id',
            titleName: 'name'
        }
    });
    function setPermissionsInsSelected(data) {
        permissionsIns.set(data.selected);
    }

});